import socket
import os
    
class server():
    def __init__(self) -> None:
        self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.server_socket.bind(("0.0.0.0", 8080))
        self.server_socket.listen(128) # 超过128的会连接不上
        
    def accept(self):
        client_socket , client_addr = self.server_socket.accept()
        return client_socket

    def send_data(self, client_socket, data):
        client_socket.send(data.encode("utf8")) 
    
    def get_data(self, client_socket):
        data = client_socket.recv(1024).decode("utf8")
        return data
    
    def close(self):
        self.server_socket.close()

if __name__ == "__main__":
    server1 = server()
    client1 = server1.accept()
    data = "client1"
    server1.send_data(client_socket=client1, data=data)
    data = server1.get_data(client_socket=client1)
    print(data)

    client1 = server1.accept()
    data = "client2"
    server1.send_data(client_socket=client1, data=data)
    data = server1.get_data(client_socket=client1)
    print(data)

    client1 = server1.accept()
    data = "client3"
    server1.send_data(client_socket=client1, data=data)
    data = server1.get_data(client_socket=client1)
    print(data)

    client1 = server1.accept()
    data = "client4"
    server1.send_data(client_socket=client1, data=data)
    data = server1.get_data(client_socket=client1)
    print(data)


    server1.close()
